<?Lassoscript
// Last modified 11/30/09 by Eric Landmann

// FUNCTIONALITY
// This establishes the queries and results pages for the admin Forums area
// Include for CustomerInfo search results
// The controlling record is Section_ID from the users table, which is passed as the action_param "ID"

// CHANGE NOTES
// 4/17/09
// Version 1.0
// 11/30/09
// Worked on admin area

If: !(Var_Defined:'SQLQueryCustomerInfo'); Session_AddVariable: -Name='forums_admin', 'SQLQueryCustomerInfo'; /If;

// Setting Variables for Nav Control
Var:'vNew' = (Action_Param:'New');
Var:'Records_Skipped' = (Action_Param:'skip');
Var:'This_Group' = (Action_Param:'group');
Var:'Records_Per_Page' = 10;
Var:'SkipCount' = integer;
Var:'Records_Found' = integer;
Var:'Group_Size' = (Math_Mult:(Var:'Records_Per_Page'),5);

// Debugging
// Var:'svDebug' = 'Y';

// Output debugging filename
If: $svDebug == 'Y';
	'<br>\n';
	'<strong>info_searchresults.inc</strong><br>\n';
/If;

// If a new query, initialize all the variables
If: Var:'vNew' == 'Y';
	Var:'vName' = (Action_Param:'Name');
	Var:'vComment' = (Action_Param:'Comment');
	Var:'vDisplay' = (Action_Param:'Display');

	Var:'SQLName' = string;
	Var:'SQLComment' = string;
	Var:'SQLDisplay' = string;
	Var:'SQLQueryCustomerInfo' = string;

	Var:'svSearchParams' = '';
	Var:'svSearchParamsURL' = '';

	// Include the code for building the search params phrase
	Include:($svLibsPath) 'searchparamsinfo_build.inc';

/If;

If: Var:'SQLQueryCustomerInfo' == '';
	Var:'SQLQueryCustomerInfo' = 'SELECT * FROM ' $svContactInfoTable ' WHERE ';

	// Name
	If: Var:'vName' != '';
		Var:'SQLName' = 'Name LIKE "%' $vName;
		$SQLQueryCustomerInfo += $SQLName '%"';
		If:  Var:'vComment' != '' || Var:'vDisplay' != '';
			$SQLQueryCustomerInfo += ' AND ';
		/If;
	/If;

	// Comment
	If: Var:'vComment' != '';
		Var:'SQLComment' = 'COMMENT LIKE "%' $vComment;
		$SQLQueryCustomerInfo += $SQLComment '%"';
		If: Var:'vDisplay' != '';
			$SQLQueryCustomerInfo += ' AND ';
		/If;
	/If;

	// Display
	If: Var:'vDisplay' != '';
		Var:'SQLDisplay' = 'DISPLAY = "' $vDisplay;
		$SQLQueryCustomerInfo += $SQLDisplay '"';
	/If;


	// SEARCHES EVERYTHING
	If: ($vName == '') && ($vComment == '') && ($vDisplay == '');
		Var:'SQLQueryCustomerInfo' = 'SELECT * FROM ' $svContactInfoTable;
	/If;

	Debug;
		'53: vName = ' (Var:'vName') '<br>\n';
		'53: vComment = ' (Var:'vComment') '<br>\n';
		'53: vDisplay = ' (Var:'vDisplay') '<br>\n';
		'53: SQLQueryCustomerInfo = ' (Var:'SQLQueryCustomerInfo') '<br>\n';
		'53: svSearchParams = ' (Var:'svSearchParams') '<br>\n';
		'53: svSearchParamsURL = ' (Var:'svSearchParamsURL') '<br>\n';
	/Debug;

/If;

Inline: $IV_SearchSections,
	-SkipRecords=(Var:'Records_Skipped'),
	-MaxRecords=(Var:'Records_Per_Page'),
	-SQL=$SQLQueryCustomerInfo;

	// Build the Nav Control Links
	Include:($svLibsPath) 'nav_control_admin.inc';

	Debug;
		'67: Error = ' (Error_CurrentError) '<br>\n';
		'67: Found_Count = ' (Found_Count) '<br>\n';
	/Debug;

	// If no records found, immediate redirect with error to setup_edit.lasso
	If: (Found_Count) == 0;
		Var:'vError'='2000';
		Var:'URL' = ('setup_edit.lasso?Error='$vError'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			Debug;
				'76: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;

	// If only one record, redirect immediately to setup_editrecord.lasso
	// This shortcut skips past the search results page if there is only one record found
	// Example URL:
	// http://127.0.0.1/admin/setup_editrecord.lasso?DataType=Info&ID=2&New=Y
	// If no records found, immediate redirect with error
	If: (Found_Count) == 1;
		Var:'vID'=(Field:'ID');
		Var:'URL' = ('setup_editrecord.lasso?DataType=Info&ID='($vID)'&New=Y');
		If: $svDebug == 'Y';
			Debug;
				'96: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;
?><table border="0" cellpadding="0" cellspacing="0" width="525" bgcolor="black">
	<tr>
		<td width="620" valign="top" bgcolor="#FFFFFF">
			<div align="left">
				<h2>Info Search Results</h2>
				<p><font color="red">[Var:'svSearchParams', -EncodeNone]</font>&nbsp;
				<a href="[$svSiteFolderRoot]/admin/setup_edit.lasso[$svSearchParamsURL]">Search Again</a></p>
			</div>
			<div align="left">
				<table width="535" cellspacing="0" cellpadding="5">
					<tr bgcolor="#000000">
						<td valign="middle" width="40" class="tablehead_10_white">ID</td>
						<td valign="middle" width="150" class="tablehead_10_white">Name</td>
						<td valign="middle" width="155" class="tablehead_10_white">Email</td>
						<td valign="middle" width="175" class="tablehead_10_white">Address</td>
					</tr>
<?LassoScript
		Records;
			Var:'vID' = (Field:'ID');
			Var:'vName' = (Field:'Name');
			Var:'vEmail' = (Field:'Mail');
			Var:'vAddress' = (Field:'Address');
?>
				<tr <?Lassoscript If: (Loop_Count) %2 == 0; 'bgcolor = "#F5F5F5"'; Else; 'bgcolor = "#FFFFFF"'; /If; ?> >
					<td valign="middle" width="40" class="tabletext_10_black">
						[Var:'vID']</td>
					<td valign="middle" width="150" class="tabletext_10_black">
						<strong><a href="setup_editrecord.lasso?DataType=Info&ID=[Var:'vID']&New=Y">[Var:'vName']</a></strong></td>
					<td valign="middle" width="155" class="tabletext_10_black">
						[Var:'vEmail']
					</td>
					<td valign="middle" width="175" class="tabletext_10_black">
						[Var:'vAddress']
					</td>
				</tr>
		[/Records]
	<tr>
		<td colspan="5" valign="middle" bgcolor="#cccccc">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="5" valign="middle" align="left" bgcolor="#f5f5f5">
			[Include:($svLibsPath) 'nav_control.inc']
		</td>
	</tr>
		</table>
[/Inline]
